package com.chuantou.yuba.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.chuantou.yuba.YBApp;
import com.chuantou.yuba.ui.LoginActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

	private void handleIntent(Intent paramIntent) {
		if (YBApp.sWXApi != null) {
			YBApp.sWXApi.handleIntent(paramIntent, this);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handleIntent(getIntent());
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		handleIntent(intent);
	}

	@Override
	public void onReq(BaseReq request) {
		finish();
	}

	@Override
	public void onResp(BaseResp response) {
		switch (response.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			if (ConstantsAPI.COMMAND_SENDAUTH == response.getType()) {
				String code = ((SendAuth.Resp) response).code;
				LoginActivity.onWXLoginCallback(code);
				break;
			}
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			break;
		default:
			break;
		}
		finish();
	}
}